package com.wannengbang.cloudleader.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.ExchangeListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 订单列表
 */
public class ExchangeOtherListAdapter extends RecyclerView.Adapter<ExchangeOtherListAdapter.ViewHolder> {

    private List<ExchangeListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
//    private int agent_id;

    public ExchangeOtherListAdapter(List<ExchangeListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
//        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
//        if (userInfoBean != null) {
//            agent_id = userInfoBean.getData().getId();
//        }
    }

    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_exchange_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //状态:1-已下单,2-已支付,3-已发货,4-已签收, 9-已取消
        ExchangeListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        //1待发货 2已发货 3已收货
        holder.tvPrepareGoods.setVisibility(View.GONE);
        holder.tvBtnDelivery.setVisibility(View.GONE);
        holder.tvBtnSign.setVisibility(View.GONE);

        if (dataBean.getStatus() == 1) {
            holder.tvOrderStatus.setText("待发货给(" + dataBean.getName() + ")");
            holder.tvPrepareGoods.setVisibility(View.VISIBLE);
            holder.tvBtnDelivery.setVisibility(View.VISIBLE);
        } else if (dataBean.getStatus() == 2) {
            holder.tvOrderStatus.setText("等待(" + dataBean.getName() + ")收货");
            holder.tvBtnSign.setVisibility(View.GONE);
        } else if (dataBean.getStatus() == 3) {
            holder.tvOrderStatus.setText("已完成");
        }

        holder.tvNumber.setText(dataBean.getNum());

        holder.tvPrepareGoods.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvBtnDelivery.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
        holder.tvBtnSign.setOnClickListener(v -> {
            onCallBackListener.onCallBack(3, position);
        });

        holder.itemView.setOnClickListener(v -> {
            onCallBackListener.onCallBack(4, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_order_status)
        TextView tvOrderStatus;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_btn_sign)
        TextView tvBtnSign;
        @BindView(R.id.tv_prepare_goods)
        TextView tvPrepareGoods;
        @BindView(R.id.tv_btn_delivery)
        TextView tvBtnDelivery;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
